package com.project.movie.controller.rotation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.BusinessType;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieRotation;
import com.project.movie.domain.Result;
import com.project.movie.service.MovieRotationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 轮播图controller
 * @date 2025/02/07 10:54
 */
@Controller
@ResponseBody
@RequestMapping("rotation")
public class MovieRotationController {

    @Autowired
    private MovieRotationService movieRotationService;

    /** 分页获取轮播图 */
    @PostMapping("getMovieRotationPage")
    public Result getMovieRotationPage(@RequestBody MovieRotation movieRotation) {
        Page<MovieRotation> page = new Page<>(movieRotation.getPageNumber(),movieRotation.getPageSize());
        QueryWrapper<MovieRotation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(movieRotation.getImage()),MovieRotation::getImage,movieRotation.getImage());
        Page<MovieRotation> movieRotationPage = movieRotationService.page(page, queryWrapper);
        return Result.success(movieRotationPage);
    }

    @GetMapping("getMovieRotationList")
    public Result getMovieRotationList() {
        List<MovieRotation> rotationList = movieRotationService.list();
        return Result.success(rotationList);
    }

    /** 根据id获取轮播图 */
    @GetMapping("getMovieRotationById")
    public Result getMovieRotationById(@RequestParam("id")String id) {
        MovieRotation movieRotation = movieRotationService.getById(id);
        return Result.success(movieRotation);
    }

    /** 保存轮播图 */
    @PostMapping("saveMovieRotation")
    public Result saveMovieRotation(@RequestBody MovieRotation movieRotation) {
        boolean save = movieRotationService.save(movieRotation);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑轮播图 */
    @PostMapping("editMovieRotation")
    public Result editMovieRotation(@RequestBody MovieRotation movieRotation) {
        boolean save = movieRotationService.updateById(movieRotation);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除轮播图 */
    @GetMapping("removeMovieRotation")
    public Result removeMovieRotation(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                movieRotationService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("轮播图id不能为空！");
        }
    }

}
